<!--水平基本柱状图-->

<template>
  <div class="w-h-full">
    <!--组件遮罩层，用于在设计器中显示-->
    <div v-if="shade" class="shade"></div>

    <div v-resize="resizeHandle" style="width: 100%; height: 100%;">
      <div :id="'chart-'+component.id+'-'+chartDomNum" class="w-h-full"></div>
    </div>
  </div>
</template>

<script>

  import FuncCompMixin from '@/mixin/FuncCompMixin'
  import ChartCompMixin from '@/mixin/ChartCompMixin'
  import PnDesigner from '@/utils/PnDesigner'

  import AcrossBaseBarChart from './AcrossBaseBarChart'

  const _this = {
    name: 'AcrossBaseBarChart', // 此名称必须填写，且必须与组件文件名相同
    mixins: [FuncCompMixin, ChartCompMixin],
    attr: {
      version: '2.4',
      configDataTemp: Object.assign({

        // 组件相关配置属性，Echarts图表组件的相关配置属性，必须在chartOption对象下定义
        chartOption: {
          title: PnDesigner.buildTitleConfigData(),
          tooltip: PnDesigner.buildTooltipConfigData({
            axisPointer: {
              type: 'shadow'
            }
          }),
          legend: PnDesigner.buildLegendConfigData(),
          grid: PnDesigner.buildGridConfigData(),
          xAxis: PnDesigner.buildYAxisConfigData(),
          yAxis: PnDesigner.buildXAxisConfigData(),
          visualMap: [],
          series: [
            AcrossBaseBarChart.buildSeriesObj('2019年', '1'),
            AcrossBaseBarChart.buildSeriesObj('2020年', '1'),
          ]
        }

      }, PnDesigner.buildFuncCompDatasourceField({ // 如果组件需要配置数据源，则需要调用此函数构建数据源字段对象
        ds_resultObjTemplate: [
          {
            field: 'name',
            remark: '类目'
          },
          {
            field: 'value',
            remark: '值'
          },
          {
            field: 'series',
            remark: '系列'
          }
        ],
        ds_resultObj: [
          {
            name: '广西',
            value: 18203,
            series: '2019年'
          },
          {
            name: '广东',
            value: 30293,
            series: '2019年'
          },
          {
            name: '上海',
            value: 40593,
            series: '2019年'
          },
          {
            name: '云南',
            value: 23223,
            series: '2019年'
          },
          {
            name: '北京',
            value: 69534,
            series: '2019年'
          },
          {
            name: '广西',
            value: 50493,
            series: '2020年'
          },
          {
            name: '广东',
            value: 12232,
            series: '2020年'
          },
          {
            name: '上海',
            value: 54643,
            series: '2020年'
          },
          {
            name: '云南',
            value: 95843,
            series: '2020年'
          },
          {
            name: '北京',
            value: 48573,
            series: '2020年'
          },
        ],
        ds_useFilter: true, // 是否使用过滤器，默认不使用
        ds_useTimer: true, // 是否开启定时器配置，默认不开启
      }), PnDesigner.buildFuncCompGlobalField(), PnDesigner.buildEchartCompCustomOption(), PnDesigner.buildFuncCompInteractionConfiguration([
        PnDesigner.buildFuncCompInteractionStreamItem('itemClick', [
          PnDesigner.buildFuncCompInteractionFieldsMapItem('name', '', '类目'),
          PnDesigner.buildFuncCompInteractionFieldsMapItem('value', '', '值'),
          PnDesigner.buildFuncCompInteractionFieldsMapItem('series', '', '系列'),
        ])
      ]))
    },
    data() {
      return {}
    },
    mounted() {
      this.init();

      this.setTimer(() => { //定时器，如果当前组件不使用定时器，可以将此段代码删除
        this.init()
      })
    },
    methods: {
      init() {
        this.initDatasource(() => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        }, (result) => {
          this.chart.setOption(this.chartOption);
          this.runCustomGlobalJsCode();
          this.runDataItemClickJsCode(this.launchInteractionStream)
        });
      },

      launchInteractionStream (params) {
        this.$PnDesigner.launchInteractionStream(this.component.compConfigData, 'itemClick', params.data)
      }
    },
    computed: {
      chartOption () {

        let option = {
          title: PnDesigner.buildTitleChartOption(this.component.compConfigData.chartOption.title),
          tooltip: PnDesigner.buildTooltipChartOption(this.component.compConfigData.chartOption.tooltip),
          legend: PnDesigner.buildLegendChartOption(this.component.compConfigData.chartOption.legend),
          grid: PnDesigner.buildGridChartOption(this.component.compConfigData.chartOption.grid),
          xAxis: PnDesigner.buildYAxisChartOption(this.component.compConfigData.chartOption.xAxis),
          yAxis: PnDesigner.buildXAxisChartOption(this.component.compConfigData.chartOption.yAxis),
          visualMap: PnDesigner.buildVisualMapChartOption(this.component.compConfigData.chartOption.visualMap, {dimension: 0}),
          series: []
        };

        // 初始化纵坐标标签
        this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'name').forEach(item => {
          option.yAxis.data.push(item.type)
        });

        /*this.component.compConfigData.chartOption.series.forEach((item, index) => {
          let groupItem = this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'series')[index];
          if (groupItem) {
            option.series.push({
              name: item.name,
              stack: item.stack,
              type: 'bar',
              label: {
                show: item.label.show,
                position: item.label.position,
                color: item.label.color,
                fontSize: item.label.fontSize
              },
              itemStyle: {
                color: item.itemStyle.color,
                barBorderRadius: item.itemStyle.barBorderRadius ? item.itemStyle.barBorderRadius : 0
              },
              barWidth: this.component.compConfigData.chartOption.global.barWidth,
              data: groupItem.list
            });

          }
          option.legend.data.push(item.name)
        });*/

        this.$PnUtil.groupArray(this.runCustomFilter(this.component.compConfigData.ds_resultObj), 'series').forEach((resultObjItem, index) => {
          if (resultObjItem) {
            let item = this.component.compConfigData.chartOption.series[index];
            if (item) {
              option.series.push({
                name: item.name ? item.name : resultObjItem.type,
                type: item.type,
                stack: item.stack,
                showBackground: item.showBackground,
                backgroundStyle: {
                  color: item.backgroundStyle.color,
                  borderColor: item.backgroundStyle.borderColor,
                  borderWidth: item.backgroundStyle.borderWidth,
                  borderType: item.backgroundStyle.borderType,
                  borderRadius: item.backgroundStyle.borderRadius,
                  shadowBlur: item.backgroundStyle.shadowBlur,
                  shadowColor: item.backgroundStyle.shadowColor
                },
                label: {
                  show: item.label.show,
                  position: item.label.position,
                  rotate: item.label.rotate,
                  formatter: item.label.formatter,
                  color: item.label.color,
                  fontSize: item.label.fontSize
                },
                itemStyle: {
                  color: item.itemStyle.color,
                  borderColor: item.itemStyle.borderColor,
                  borderWidth: item.itemStyle.borderWidth,
                  borderType: item.itemStyle.borderType,
                  barBorderRadius: item.itemStyle.barBorderRadius,
                  shadowBlur: item.itemStyle.shadowBlur,
                  shadowColor: item.itemStyle.shadowColor
                },
                barWidth: item.barWidth,
                barGap: item.barGap,
                barCategoryGap: item.barCategoryGap,
                data: resultObjItem.list
              });
              option.legend.data.push(item.name ? item.name : resultObjItem.type)
            }else {
              option.series.push(AcrossBaseBarChart.buildDefaultSeriesObj(resultObjItem.type, resultObjItem.list))
              option.legend.data.push(resultObjItem.type)
            }
          }
        })

        // 下面这一段代码用于合并组件的扩展配置，如果组件不需要扩展配置，可以删掉
        option = this.$PnDesigner.mergeChartCustomOption(option, this.component.compConfigData.customOption);

        return option;
      }
    }
  };

  export default _this
</script>

<style scoped>

</style>
